package utilPartTwo;

import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;

public class AvgNums {
    public static void main(String[] args) throws Exception {
        FileWriter fw=new FileWriter("text.txt");
        fw.write("2, 3.4, 5, 6, 7.4, 9.1, 10.5, done");
        fw.close();

        FileReader fr=new FileReader("text.txt");
        Scanner conin=new Scanner(fr);
        conin.useDelimiter(", *");

        int count=0;
        double sum=0.0;

        while (conin.hasNext()){
            if (conin.hasNextDouble()){
                sum += conin.nextDouble();
                count++;
            }
            else {
                String str=conin.next();
                if (str.equals("done")){
                    break;
                }
                else {
                    System.out.println("Data format error.");
                    return;
                }
            }
        }
        conin.close();
        System.out.println("Average is "+sum/count);
    }
}

